package com.xuanyuan.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.xuanyuan.dao.UserDao;
import com.xuanyuan.model.Departments;
import com.xuanyuan.model.User;
import com.xuanyuan.util.Model2VoUtil;
import com.xuanyuan.util.PageInfo;
import com.xuanyuan.vo.UserVo;

@Component("userService")
public class UserService {
    UserDao userDao;
    
    public Boolean isLogin(String userName, String password){
    	return userDao.isLogin(userName, password);
    }
    
    public void addUser(User user) {
    	userDao.addUser(user);
    }
    
    public List<UserVo> listUser(PageInfo pageInfo) {
    	List<User> lists = userDao.listUser(pageInfo);
    	System.out.println(lists);
    	List<UserVo> userVo = new ArrayList<UserVo>();
    	for(User user : lists){
    		UserVo uVo = Model2VoUtil.getUserVo(user);
    		userVo.add(uVo);
    	}
    	 return userVo;
    }

    public UserVo selectUser(String userName) {
    	User u = userDao.selectUser(userName);
    	if(u != null){
    		return Model2VoUtil.getUserVo(u);
    	} else {
    		return null;
    	}
    }

    public void updatePass(Integer userId, String password) {
    	 userDao.updatePass(userId, password);
    }
    
    public void updateUser(Integer userId, String userName, String name,
			String department, String phone, String email) {
    	userDao.updateUser(userId, userName, name, department, phone, email);
    }
    
    public void delUser(User user){
    	userDao.delUser(user);
    }
    
    public UserVo getUserVo(Integer userId){
    	User user = userDao.getUsers(userId);
    	UserVo uVo = Model2VoUtil.getUserVo(user);
    	return uVo;
    }
    
    public User getUsers(Integer userId){
    	return userDao.getUsers(userId);
    }
    
	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
    
	public void addUserToDepartment(Departments user){
		userDao.addUserToDepartment(user);
	}
    
	public void updatePass(String password, String username){
		userDao.updatePass(password, username);
	}
	
	public User selectPass(String password, String username){
		return userDao.selectPass(password, username);
	}
	
	public List<UserVo> getListUserByName(String username, PageInfo pageInfo){
		List<User> list = userDao.getListUser(username, pageInfo);
		List<UserVo> uVo = new ArrayList<UserVo>();
		for(User u : list){
			UserVo uo = Model2VoUtil.getUserVo(u);
			System.out.println(u);
			uVo.add(uo);
		}
		return uVo;
	}
}
